<?php
function random_float ($min,$max) {
   return ($min+lcg_value()*(abs($max-$min)));
}
// Configuration
//http://www.franzone.com/2008/05/20/how-do-i-get-meta-data-from-mysql-using-php/

$host = 'localhost';
$user = 'root';
$pass = '';
$database = 'cc_V1';
$numberOfRecs = 500;
$countries = array("Australia", "Bangladesh", "England", "India", "New Zealand", "Pakistan", "South Africa", "Sri Lanka", "West Indies", "Zimbabwe");
$playerTypes = array("Batsman","Bowler", "AllRounder","WicketKeeper");
// Connect to the MySQL database
$conn = mysql_connect($host, $user, $pass) or die("Could not connect to database\n");
mysql_select_db($database, $conn) or die("Could not connect to $database\n");


// Query to get list of tables
$result = mysql_query("SHOW TABLES FROM $database", $conn);
if ($result) {
	$output = array ();
	while ($row = mysql_fetch_array($result)) {
		$tableName = $row[0];

		$cols = mysql_query("SHOW COLUMNS FROM $tableName", $conn);
		if ($cols) {
			while ($col = mysql_fetch_assoc($cols)) {
				if (!array_key_exists($tableName, $output)) {
					$output[$tableName] = array ();
				}
				$output[$tableName][$col['Field']] = $col['Type'];
			}
			mysql_free_result($cols);
		}
	}
	// Output results
	/*foreach($output as $table => $cols)
	{
	  echo "===== $table =====\n";
	  foreach($cols as $field => $type)
	  {
	    echo "$field : $type\n";
	  }
	}*/
}
echo "delete FROM performance;"."<br>";
echo "delete FROM player;"."<br>";
echo "delete FROM stat;"."<br>";
echo "delete FROM team;"."<br>";
echo "delete FROM users;"."<br>";

foreach ($output as $table => $cols) {
	for ($index = 0; $index < $numberOfRecs; $index++) {
		$query = "Insert into " . $table ;
		$firstPart= "( ";
		$secondPart= "( ";
		foreach ($cols as $field => $type) {
			if(strcasecmp($field,"id")!=0){
			$firstPart = $firstPart.$field. ", ";  
			if (strpos($type, 'int')==4) {
				$secondPart = $secondPart."'".  '1'."' ,";
			} else if (strpos($type, 'char')==3) {
				if((strpos($field, 'ountry')!=0)){
					$secondPart = $secondPart . " '".$countries[mt_rand(0,9)]. "' ,";
				}else if ((strpos($field, 'layerType')!=0)){
					$secondPart = $secondPart . " '".$playerTypes[mt_rand(0,3)]. "' ,";
				}else if ((strpos($field, 'id')==(strlen($field)-2))){
					$secondPart = $secondPart . " '".mt_rand(0,1000). "' ,";
				}else{
					$secondPart = $secondPart . " '".$field .$index. "' ,";
				}
			} else if (strpos($type, 'time')==4) {
				if((strpos($field, 'of_birth')==5)){
					$time = mt_rand( time()-(20000 * 24 * 60 * 60), time()-(1000 * 24 * 60 * 60) );
				}else{
					$time = mt_rand( time()-(100 * 24 * 60 * 60), time() );	
				}
				$dt = date("m-d-Y", $time);
				$secondPart= $secondPart. " '".$dt. "' ,";
			}else if (strpos($type, 'loat')==1) {
				$number= round(random_float(0,10),2);
				$secondPart =$secondPart. " '" .$number. "' ,";
			}
			else if (strpos($type, 'nt')==1) {
				$number= mt_rand(0,1000);
				$secondPart =$secondPart . " '" .$number. "' ,";
			}
		}
		}
		$firstPart = substr($firstPart , 0, -2);
		$secondPart = substr($secondPart , 0, -1);
		$query = $query .$firstPart.")". ' values '.$secondPart.")";
		$query = substr($query , 0, -1);
		$query = $query." );";
		echo $query . "<br>";
	}

}
// Free result
if ($result)
	mysql_free_result($result);
// Close connection
if ($conn)
	mysql_close($conn);
?>